package commons.logs;

import commons.logs.Logger.Level;
import java.util.ArrayList;

public class DlgLogs extends javax.swing.JDialog
{

	private final ArrayList<String> vLogs;
	private final ArrayList<Level> vLevels;
	
	public DlgLogs(java.awt.Frame parent, boolean modal, ArrayList<String> vLogs, ArrayList<Level> vLevels) {
		super(parent, modal);
		this.vLogs = vLogs;
		this.vLevels = vLevels;
		initComponents();
		
		refresh();
	}

	private void refresh() {
		
		StringBuilder sb = new StringBuilder();
		int nbLine = vLogs.size();
		
		for (int i = 0; i<nbLine; i++) {
			String msg = vLogs.get(i);
			Level level = vLevels.get(i);
			
			if ((level == Level.ERROR && toggle_Error.isSelected()) ||
				(level == Level.WARN_ && toggle_Warning.isSelected()) ||
				((level == Level.INFO1||level == Level.INFO2) && toggle_Info.isSelected()) ||
				((level == Level.DEBUG1||level == Level.DEBUG2) && toggle_Debug.isSelected()) ) {
				
				sb.append(msg).append('\n');
				
			}
		}
		
		jTextArea1.setText(sb.toString());
	}
	

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        toggle_Error = new javax.swing.JToggleButton();
        toggle_Warning = new javax.swing.JToggleButton();
        toggle_Info = new javax.swing.JToggleButton();
        toggle_Debug = new javax.swing.JToggleButton();
        toggle_Close = new javax.swing.JButton();

        setTitle("Logs du dernier traitement");

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        toggle_Error.setSelected(true);
        toggle_Error.setText("Erreurs");
        toggle_Error.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle_ErrorActionPerformed(evt);
            }
        });

        toggle_Warning.setSelected(true);
        toggle_Warning.setText("Warnings");
        toggle_Warning.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle_WarningActionPerformed(evt);
            }
        });

        toggle_Info.setText("Information");
        toggle_Info.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle_InfoActionPerformed(evt);
            }
        });

        toggle_Debug.setText("Debug");
        toggle_Debug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle_DebugActionPerformed(evt);
            }
        });

        toggle_Close.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        toggle_Close.setText("FERMER");
        toggle_Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle_CloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(toggle_Error)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toggle_Warning, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toggle_Info, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toggle_Debug, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(toggle_Close)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 456, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(toggle_Error)
                    .addComponent(toggle_Warning)
                    .addComponent(toggle_Info)
                    .addComponent(toggle_Debug)
                    .addComponent(toggle_Close))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void toggle_CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggle_CloseActionPerformed
        setVisible(false);
    }//GEN-LAST:event_toggle_CloseActionPerformed

    private void toggle_ErrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggle_ErrorActionPerformed
        refresh();
    }//GEN-LAST:event_toggle_ErrorActionPerformed

    private void toggle_WarningActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggle_WarningActionPerformed
        refresh();
    }//GEN-LAST:event_toggle_WarningActionPerformed

    private void toggle_InfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggle_InfoActionPerformed
        refresh();
    }//GEN-LAST:event_toggle_InfoActionPerformed

    private void toggle_DebugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggle_DebugActionPerformed
        refresh();
    }//GEN-LAST:event_toggle_DebugActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton toggle_Close;
    private javax.swing.JToggleButton toggle_Debug;
    private javax.swing.JToggleButton toggle_Error;
    private javax.swing.JToggleButton toggle_Info;
    private javax.swing.JToggleButton toggle_Warning;
    // End of variables declaration//GEN-END:variables
}
